﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Common;
using CIT.WMS.EnumType;

namespace CIT.WMS.Msd
{
    public partial class MSD_Open_Manage : Form
    {
        T_Bllb_MSDMain_tbmm _msdMain_tbmm { get; set; }

        DataTable dtMsd = new DataTable();

        public MSD_Open_Manage()
        {
            InitializeComponent();
        }

        private void txtSerialNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (!ValidateInput())
                {
                    txtSerialNumber.SelectAll();
                    txtSerialNumber.Focus();
                    return;
                }
                _msdMain_tbmm = new T_Bllb_MSDMain_tbmm();
                _msdMain_tbmm.SerialNumber = txtSerialNumber.Text.Trim();
                _msdMain_tbmm.MaterialCode = ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]);
                _msdMain_tbmm.Status = ((int)MSD_Main_Enum.Open).ToString();
                _msdMain_tbmm.Remark = string.Empty;
                MSDService.SaveOpenRecords(_msdMain_tbmm);
                PublicUtils.ShowMsg(this.lab_msg, "拆封成功", true);
                PublicUtils.WriteLog(txtSerialNumber.Text.Trim(), "拆封", ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]), ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["QTY"], 0));
                txtSerialNumber.Text = string.Empty;
                txtSerialNumber.Focus();
            }
        }

        private bool ValidateInput()
        {
            if (string.IsNullOrEmpty(txtSerialNumber.Text.Trim()))
            {
                PublicUtils.ShowMsg(this.lab_msg, "ReelID不能为空", false);
                return false;
            }
            StringBuilder str_where = new StringBuilder();
            str_where.AppendFormat(" and a.SerialNumber='{0}'", txtSerialNumber.Text.Trim());
            dtMsd = MSDService.Query(str_where.ToString());
            if (dtMsd.Rows.Count == 0)//校验库存表是否存在该条码
            {
                PublicUtils.ShowMsg(this.lab_msg, "ReelID错误", false);
                return false;
            }
            else
            {
                if (!(ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Lock_Flag"]) == "0" || ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Lock_Flag"]) == "7"))//校验状态
                {
                    PublicUtils.ShowMsg(this.lab_msg, "ReelID状态错误", false);
                    return false;
                }
                if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["IsMSD"]) != "0")//校验是否是MSD
                {
                    PublicUtils.ShowMsg(this.lab_msg, "ReelID非MSD", false);
                    return false;
                }
                if (ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) != string.Empty && ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) != "4" && ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["Status"]) != "2")
                {
                    PublicUtils.ShowMsg(this.lab_msg, "ReelID当前状态不允许拆封", false);
                    return false;
                }
                if (ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["SplitTime"], 0) != 0)
                {
                    if (ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["OpenNum"], 0) >= ConvertUtils.ChangeNullToInt(dtMsd.Rows[0]["SplitTime"], 0))
                    {
                        PublicUtils.ShowMsg(this.lab_msg, "ReelID已达到最大拆封次数", false);
                        return false;
                    }
                }
            }
            return true;
        }

        //private void txtRemark_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (e.KeyChar == 13)
        //    {
        //        if (!ValidateInput())
        //        {
        //            txtSerialNumber.SelectAll();
        //            txtSerialNumber.Focus();
        //            return;
        //        }
        //        _msdMain_tbmm = new T_Bllb_MSDMain_tbmm();
        //        _msdMain_tbmm.SerialNumber = txtSerialNumber.Text.Trim();
        //        _msdMain_tbmm.MaterialCode = ConvertUtils.ChangeNullToString(dtMsd.Rows[0]["MaterialCode"]);
        //        _msdMain_tbmm.Status = ((int)MSD_Main_Enum.Open).ToString();
        //        _msdMain_tbmm.Remark = txtRemark.Text.Trim();
        //        MSDService.SaveOpenRecords(_msdMain_tbmm);
        //        PublicUtils.ShowMsg(this.lab_msg, "拆封成功", true);
        //        txtSerialNumber.Text = string.Empty;
        //        txtRemark.Text = string.Empty;
        //        txtSerialNumber.Focus();
        //    }
        //}
    }
}